program ICQ_RemoteCP_UsersConverter;

{$APPTYPE CONSOLE}

uses
  Tokens in 'Utils\Tokens.pas',
  Classes,
  IniFiles,
  SysUtils,
  StrUtils,
  ICQ_RemoteCP_Settings in 'ICQ_RemoteCP_Settings.pas',
  ICQ_RemoteCP_Users in 'ICQ_RemoteCP_Users.pas',
  StringsSettings in 'Utils\StringsSettings.pas',
  xIniFile in 'Utils\xIniFile.pas';

const
  progName = 'Users Converter for ICQ RemoteCP (from 2.3.x and older to 2.4)';
  myPluginName = 'ICQ RemoteCP';
  //old data
  ssAdmins = 'Admins';
  sAdmin = 'Admin';
  sAllowed = 'Allowed';
  sAdminsList = 'AdminsList';

var
  IniFile: TIniFile;
  List: TStrings = nil;
  ListID: TStrings = nil;
  Path, s, UIN, SupID: String;
  i, j: Integer;

begin
 writeln(progName);
 Path:=IncludeTrailingPathDelimiter(ExtractFilePath(ParamStr(0)));
 if FileExists(Path+myPluginName+'.ini') then
  begin
   IniFile:=TIniFile.Create(Path+myPluginName+'.ini');
   ListID:=TStringList.Create;
   if IniFile.SectionExists(ssUsers) then
    begin
     //2.4 users search
     writeln('Search for existing 2.4 users...');
     i:=0;
     repeat
      s:=IniFile.ReadString(ssUsers, ssUsers+IntToStr(i), '');
      if s<>'' then
       begin
        if TokenExist(tknID, s) and TokenExist(tknType, s) then
         begin
          ListID.Add(s);
          writeln('Found valid ID '+UserID(s)+'.');
         end
        else
         begin
          if IsUIN(s) then
           begin
            ListID.Add(NewUserStr(s, utUser));
            writeln('Found UIN '+s+'. Converted to valid ID.');
           end
          else
           begin
            IniFile.DeleteKey(ssUsers, ssUsers+IntToStr(i));
            writeln('Found malformed ID. Deleted.');
           end;
         end;
       end;
      Inc(i);
     until (i>=cUsersMax) or (s='');
     writeln('Search complete. '+IntToStr(ListID.Count)+' valid ID(s) found/converted.');
    end
   else
     writeln('Warning! No Users section in Ini.');
   //2.3.x admins search
   List:=TStringList.Create;
   writeln('Search for admins...');
   SupID:=IniFile.ReadString(ssAdmins, sAdmin, '');
   if IsUIN(SupID) then
    begin
     ListID.Add(NewUserStr(SupID, utSupervisor));
     writeln('Found supervisor UIN '+SupID+'. Converted to valid ID.');
    end
   else
     writeln('Warning! No supervisor UIN found.');
   s:=IniFile.ReadString(ssAdmins, sAdminsList, '');
   List.CommaText:=s;
   if List.Count>0 then
    begin
     for i:=0 to List.Count-1 do
      begin
       if List[i]<>SupID then
        begin
         ListID.Add(NewUserStr(List[i], utAdmin));
         writeln('Found admin UIN '+List[i]+'. Converted to valid ID.');
        end;
      end;
     writeln('Search complete. '+IfThen(List.IndexOf(SupID)<0, IntToStr(List.Count), IntToStr(List.Count-1))+' UINs converted.');
    end
   else
     writeln('Warning! No Admins UINs found.');
   //2.3.x users search
   writeln('Search for users...');
   s:=IniFile.ReadString(ssUsers, sAllowed, '');
   List.CommaText:=s;
   if List.Count>0 then
    begin
     for i:=0 to List.Count-1 do
      begin
       ListID.Add(NewUserStr(List[i], utUser));
       writeln('Found user UIN '+List[i]+'. Converted to valid ID.');
      end;
     writeln('Search complete. '+IntToStr(List.Count)+' UINs converted.');
    end
   else
     writeln('Warning! No Users UINs found.');
   List.Free;
   //save users
   writeln('Saving IDs back...');
   for i:=0 to ListID.Count-1 do
    begin
     IniFile.WriteString(ssUsers, ssUsers+IntToStr(i), ListID[i]);
     writeln(UserID(ListID[i])+' saved.');
    end;
   IniFile.WriteInteger(cSetSections, ssUsers, ListID.Count);
   writeln('Done. '+IntToStr(ListID.Count)+' IDs saved.');
   ListID.Free;
   //cleanup
   writeln('Cleanup old data...');
   if IniFile.SectionExists(ssAdmins) then
    begin
     IniFile.EraseSection(ssAdmins);
     writeln('Section '+ssAdmins+' deleted.');
    end;
   if IniFile.ValueExists(ssUsers, sAllowed) then
    begin
     IniFile.DeleteKey(ssUsers, sAllowed);
     writeln('Key '+sAllowed+' deleted.');
    end;
   writeln('Done.');
   IniFile.UpdateFile;
   IniFile.Free;
  end
 else
   writeln('Error! File "'+myPluginName+'.ini" not found!');
 writeln('Press Enter to exit.');
 readln;
end.
